Crate uhlc

source ·
Expand description

A Unique Hybrid Logical Clock.

This library is an implementation of an Hybrid Logical Clock (HLC) associated to a unique identifier. Thus, it is able to generate timestamps that are unique across a distributed system, without the need of a centralized time source.

Quick Start

use uhlc::HLC;

// create an HLC with a generated UUID and relying on SystemTime::now()
let hlc = HLC::default();

// generate timestamps
let ts1 = hlc.new_timestamp();
let ts2 = hlc.new_timestamp();
assert!(ts2 > ts1);

// update the HLC with a timestamp incoming from another HLC
// (typically remote, but not in this example...)
let hlc2 = HLC::default();
let other_ts = hlc2.new_timestamp();

if ! hlc.update_with_timestamp(&other_ts).is_ok() {
    println!(r#"The incoming timestamp would make this HLC
             to drift too much. You should refuse it!"#);
}

let ts3 = hlc.new_timestamp();
assert!(ts3 > ts2);
assert!(ts3 > other_ts);

Structs

An Hybric Logical Clock generating Timestamps
The builder of HLC.
An identifier for an HLC (MAX_SIZE bytes maximum). This struct has a constant memory size (holding internally a [u8; MAX_SIZE] + a NonZeroU8), allowing allocations on the stack for better performances.
A NTP 64-bits format as specified in RFC-5909
A timestamp made of a NTP64 and a crate::HLC’s unique identifier.

Constants

The size of counter part in NTP64 (in bits)

Functions

A physical clock relying on std::time::SystemTime::now().
A dummy clock that returns a NTP64 initialized with the value 0. Suitable to use in no_std environments where std::time::{SystemTime, UNIX_EPOCH} are not available. If the feature std is disabled, that’s the default clock used by an HLC if HLCBuilder::with_clock() is not called. Notice that this means that the HLC will use incremental timestamps starting from 0.